﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Documents;
using MDSAdministration;
using MDSAdministration.mdsService;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MDSAdministrationTest
{
    [TestClass]
    public class AttributeGroupTests
    {


        [TestMethod]
        // Test Attribute Groups match from an excel file
        public void AttributeGroupTest1()
        {
            CheckStates cStates = new CheckStates();

            CompareModel modelA = new CompareModel();
            CompareModel modelB = new CompareModel();

            var c = new Common();
            c.LoadSampleData(@"..\..\TestData\ModelAttributeGroup.xlsx");
            BusinessHelper.buildModel(c.dsA, modelA);
            BusinessHelper.buildModel(c.dsB, modelB);

            var compResult = BusinessHelper.CompareModels(modelA, modelB, cStates, Common.ServerA, Common.ServerB);
            var result = compResult.ContainsKey("test");

            foreach (var co in compResult)
            {
                if (co.Value.ObjectType != Constants.OBJECT_TYPE_ENTITY)
                {
                    continue;
                }

                List<ComparedAttributeGroups> lCAG = null;
                switch (co.Value.locationType)
                {
                    case ObjectStatus.ServerA:
                        {
                            lCAG = BusinessHelper.buildServerACompareAttributeGroups(co.Value, modelA);
                        }
                        break;
                    case ObjectStatus.ServerB:
                        {
                            lCAG = BusinessHelper.buildServerBCompareAttributeGroups(co.Value, modelB);
                        }
                        break;
                    case ObjectStatus.Same:
                    case ObjectStatus.Rename:
                    case ObjectStatus.Different:
                        {
                            lCAG = BusinessHelper.buildServerSameDifferntRenameCompareAttributeGroups(co.Value, modelA, modelB, cStates);
                        }
                        break;
                }

                foreach (var cag in lCAG)
                {
                    List<ComparedAttributeGroupDetails> lCagd = null;
                    switch (cag.comparedObject.locationType)
                    {
                        case ObjectStatus.ServerA:
                            {
                                lCagd = BusinessHelper.buildServerACompareAttributeGroupDetails(cag);
                                break;
                            }
                        case ObjectStatus.ServerB:
                            {
                                lCagd = BusinessHelper.buildServerBCompareAttributeGroupDetails(cag);
                                break;
                            }
                        case ObjectStatus.Same:
                        case ObjectStatus.Different:
                        case ObjectStatus.Rename:
                            {
                                if (cag.Status == ObjectStatus.Missing && cag.AttributeGroupA != null && !string.IsNullOrEmpty(cag.AttributeGroupA.Name))
                                {
                                    lCagd = BusinessHelper.buildServerACompareAttributeGroupDetails(cag);
                                }
                                else if (cag.Status == ObjectStatus.Missing && cag.AttributeGroupB != null && !string.IsNullOrEmpty(cag.AttributeGroupB.Name))
                                {
                                    lCagd = BusinessHelper.buildServerBCompareAttributeGroupDetails(cag);
                                }
                                else
                                {
                                    lCagd = BusinessHelper.buildServerSameDifferntRenameCompareAttributeGroupDetails(cag, cStates);
                                }
                                break;
                            }
                    }
                    
                    if (cag.AttributeGroupA != null)
                    {
                        if (cag.AttributeGroupA.Name == "Test_AGA")
                        {
                            Assert.AreEqual(ObjectStatus.Missing, cag.Status);
                            Assert.AreEqual(0, lCagd.Count);
                            
                        }
                        if (cag.AttributeGroupA.Name == "Test_AGA_AGDA")
                        {
                            Assert.AreEqual(ObjectStatus.Missing, cag.Status);
                            Assert.AreEqual(1, lCagd.Count);
                            Assert.AreEqual(ObjectStatus.Missing, lCagd[0].Status);
                        }
                        if (cag.AttributeGroupA.Name == "Test_AGA_AGB")
                        {
                            Assert.AreEqual(ObjectStatus.Same, cag.Status);
                            Assert.AreEqual(0, lCagd.Count);
                        }
                        if (cag.AttributeGroupA.Name == "Test_AGA_AGDA_AGB")
                        {
                            Assert.AreEqual(ObjectStatus.Different, cag.Status);
                            Assert.AreEqual(1, lCagd.Count);
                            Assert.AreEqual(ObjectStatus.Missing, lCagd[0].Status);
                        }
                        if (cag.AttributeGroupA.Name == "Test_AGA_AGB_AGDB")
                        {
                            Assert.AreEqual(ObjectStatus.Different, cag.Status);
                            Assert.AreEqual(1, lCagd.Count);
                            Assert.AreEqual(ObjectStatus.Missing, lCagd[0].Status);
                        }
                        if (cag.AttributeGroupA.Name == "Test_AGA_AGDA_AGB_AGDB")
                        {
                            Assert.AreEqual(ObjectStatus.Same, cag.Status);
                            Assert.AreEqual(1, lCagd.Count);
                            Assert.AreEqual(ObjectStatus.Same, lCagd[0].Status);
                        }
                        if (cag.AttributeGroupA.Name == "Test_AGA_AGB_Rename")
                        {
                            Assert.AreEqual(ObjectStatus.Rename, cag.Status);
                            Assert.AreEqual(0, lCagd.Count);
                        }
                        

                    }
                    if (cag.AttributeGroupB != null)
                    {
                        if (cag.AttributeGroupB.Name == "Test_AGB")
                        {
                            Assert.AreEqual(ObjectStatus.Missing, cag.Status);
                            Assert.AreEqual(0, lCagd.Count);
                        }
                        if (cag.AttributeGroupB.Name == "Test_AGA_AGB")
                        {
                            Assert.AreEqual(ObjectStatus.Same, cag.Status);
                            Assert.AreEqual(0, lCagd.Count);
                        }
                        if (cag.AttributeGroupB.Name == "Test_AGA_AGDA_AGB")
                        {
                            Assert.AreEqual(ObjectStatus.Different, cag.Status);
                            Assert.AreEqual(1, lCagd.Count);
                            Assert.AreEqual(ObjectStatus.Missing, lCagd[0].Status);
                        }
                        if (cag.AttributeGroupB.Name == "Test_AGB_AGDB")
                        {
                            Assert.AreEqual(ObjectStatus.Missing, cag.Status);
                            Assert.AreEqual(1, lCagd.Count);
                            Assert.AreEqual(ObjectStatus.Missing, lCagd[0].Status);
                        }
                        if (cag.AttributeGroupB.Name == "Test_AGA_AGB_AGDB")
                        {
                            Assert.AreEqual(ObjectStatus.Different, cag.Status);
                            Assert.AreEqual(1, lCagd.Count);
                            Assert.AreEqual(ObjectStatus.Missing, lCagd[0].Status);
                        }
                        if (cag.AttributeGroupB.Name == "Test_AGA_AGDA_AGB_AGDB")
                        {
                            Assert.AreEqual(ObjectStatus.Same, cag.Status);
                            Assert.AreEqual(1, lCagd.Count);
                            Assert.AreEqual(ObjectStatus.Same, lCagd[0].Status);
                        }
                        if (cag.AttributeGroupB.Name == "Test_AGA_AGB_Rename1")
                        {
                            Assert.AreEqual(ObjectStatus.Rename, cag.Status);
                            Assert.AreEqual(0, lCagd.Count);
                        }
                    }
                }
            }

            Assert.AreEqual(true, true);

        }
    }
}
